//
//  TrustView.m
//  SZBank
//
//  Created by rimi on 16/7/11.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "TrustView.h"

@interface TrustView ()
{
   CGFloat frameWidth;/**< 视图宽度 */
    CGFloat frameHeight;/**< 视图高度 */
    
    CGFloat backgroundImageWidth;/**< 背景宽度 */
     CGFloat backgroundImageHeight;/**< 背景高度 */
    
  
    
}

@property (nonatomic) UIImageView *backgroundImageView;/**< 背景图 */
@property (nonatomic)UIImageView *introduceImageView;/**< 信托介绍 */
@property (nonatomic)UIScrollView *scrollView;/**< 滚动视图 */
@property (nonatomic)UIImageView *tableImageView;/**< 表格背景 */
@end
@implementation TrustView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
   
        //获取视图尺寸
        frameWidth = self.frame.size.width;
        frameHeight = self.frame.size.height;
        //获取背景图片尺寸
        backgroundImageWidth = self.backgroundImageView.frame.size.width;
        backgroundImageHeight = self.backgroundImageView.frame.size.height;
        //添加视图
        [self.scrollView addSubview:self.tableImageView];
        [self.backgroundImageView addSubview:self.introduceImageView];
        [self.backgroundImageView addSubview:self.scrollView];
        [self addSubview:self.backgroundImageView];
        
        
        [self BJNetWorking];
    }
    return self;
}
#pragma mark -- actions
//创建label
- (void)addLabelsToTableImageViewWithArray:(NSArray *)dataArray{
    //获取滚动视图的尺寸
    CGFloat scrWidth = self.tableImageView.frame.size.width;
    CGFloat scrHeiht = self.tableImageView.frame.size.height;
    
    //循环创建label
    for (int i = 0; i < 14; i ++) {
        UILabel *label = [[UILabel alloc]init];
        if (i == 0) {
            label.frame = CGRectMake(0, 0, scrWidth, scrHeiht * 0.12);
            label.text = dataArray[i];
            label.textAlignment = NSTextAlignmentCenter;
        }else if (i > 0 && i < 6){
            label.frame = CGRectMake(scrWidth* 0.5,   scrHeiht * 0.14 +scrHeiht * 0.08 * (i - 1), scrWidth * 0.46, scrHeiht * 0.07);
            label.text = dataArray[i];
        }else if (i > 5 && i < 10){
            label.frame = CGRectMake(scrWidth* 0.1,   scrHeiht * 0.28+scrHeiht * 0.075 * (i - 1), scrWidth * 0.3, scrHeiht * 0.07);
            label.text = dataArray[i];
            label.textAlignment = NSTextAlignmentCenter;
        }else{
            label.frame = CGRectMake(scrWidth* 0.58,   scrHeiht * 0.28+scrHeiht * 0.075 * (i - 5), scrWidth * 0.3, scrHeiht * 0.07);
            label.text = dataArray[i];
            label.textAlignment = NSTextAlignmentCenter;
        }
        [self.tableImageView addSubview:label];
    }
}
//网络访问
- (void)BJNetWorking{
 
    [NetWorkingDataManager getProtocolBusinessTrustDetailWithParam:@{@"id":@"402886c240e1b02c0140e2c3ea530020"} success:^(id sender) {

        //整体数据数组
        NSMutableArray *array_dataArray = [NSMutableArray array];

//遍历数据加入整体数组
        
        NSArray *keyName = @[@"NAME",@"SALEDATE",@"INVESTLIMITDATE",@"TOTALLIMIT",@"PAYMETHOD",@"TYPE"];
        
        for (int i = 0; i < 6; i ++) {
            if (i == 1) {
             NSString *subString =  [sender[@"dataDetail"][keyName[i]] substringToIndex:10];
                [array_dataArray addObject:subString ];
            }else if (i == 2){
                  [array_dataArray addObject:[NSString stringWithFormat:@"%@天",sender[@"dataDetail"][keyName[i]]] ];
            }else if (i == 3){
                 [array_dataArray addObject:[NSString stringWithFormat:@"%@元",sender[@"dataDetail"][keyName[i]]] ];
            }
            else{
            [array_dataArray addObject:sender[@"dataDetail"][keyName[i]] ];
            }
        }
        
        //遍历数据加入数组
        for (NSDictionary *dict in sender[@"extendData"]) {

            if (![dict[@"EXPECTEDRATE"] isEqualToString:@"15.0"]) {
                [array_dataArray addObject:[NSString stringWithFormat:@"%@%@",dict[@"EXPECTEDRATE"],@"%"]];
            }
        }
  
        for ( NSDictionary *dict in sender[@"extendData"]) {
            if ([dict[@"SALEMONEYBEGIN"] isEqualToString:@"10000"]) {
                if (![array_dataArray containsObject:dict[@"SALEMONEYBEGIN"]]) {
                    [array_dataArray addObject:dict[@"SALEMONEYBEGIN"]];

                }
            }else{
                NSString *subString = [dict[@"SALEMONEYBEGIN"] substringToIndex:3];
                NSString *string = [NSString stringWithFormat:@"%@万-%@万",subString,subString];
            
                [array_dataArray addObject:string];
            }
        }

        
        //添加label到视图
        [self addLabelsToTableImageViewWithArray:array_dataArray];
        
        
    } failure:^(id sender) {
         NSLog(@"fail%@",sender);
    }];
}

#pragma  mark -- getters
/**
 *  背景图片
 *
 *
 */
- (UIImageView *)backgroundImageView{
    if (!_backgroundImageView) {
        _backgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, frameHeight * 0.06, frameWidth, frameHeight * 0.9)];
        _backgroundImageView.image = [LocalDataManager getImageWithSourceOfPath:@"通用背景"];
        _backgroundImageView.userInteractionEnabled = YES;
    }
    return _backgroundImageView;
}
/**
 *  信托介绍
 */



- (UIImageView *)introduceImageView{
    if (!_introduceImageView) {
        _introduceImageView = [[UIImageView alloc]initWithFrame:CGRectMake(backgroundImageWidth * 0.54, backgroundImageHeight * 0.06, backgroundImageWidth * 0.4, backgroundImageHeight * 0.74)];
        _introduceImageView.image = [LocalDataManager getImageWithSourceOfPath:@"信托介绍"];
    }
    return _introduceImageView;
}


/**
 *  滚动视图
 */

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, backgroundImageWidth, backgroundImageHeight)];
      
        _scrollView.showsVerticalScrollIndicator = YES;
        _scrollView.contentSize = CGSizeMake(backgroundImageWidth, backgroundImageHeight * 1.1);
    }
    return _scrollView;
}


- (UIImageView *)tableImageView{
    if (!_tableImageView) {
        _tableImageView = [[UIImageView alloc]initWithFrame:CGRectMake(backgroundImageWidth * 0.08, backgroundImageHeight * 0.13, backgroundImageWidth * 0.35, backgroundImageHeight * 0.66)];
        _tableImageView.image = [LocalDataManager getImageWithSourceOfPath:@"信托_小背景"];
        
    }
    return _tableImageView;
}


@end
